﻿using AutoMapper;
using NET6.WebAPI.Entities.Base;
using NET6.WebAPI.Entities.Product;
using NET6.WebAPI.Extensions;
using SqlSugar;

namespace NET6.WebAPI.Strategies
{
    /// <summary>
    /// 品牌添加策略
    /// </summary>
    public class BrandAddStrategy : IExamineStatusStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using (var tran = db.UseTran())
            {
                var brand = mapper.Map<Sys_Brand>(examine);
                brand.CreateTime = DateTime.Now;

                int id = await db.Insertable(brand).ExecuteReturnIdentityAsync();

                examine.BusinessID = id;
                await db.Updateable(examine).ExecuteCommandAsync();

                await db.CommitTranAsync();
            }
        }
    }

    /// <summary>
    /// 品牌修改策略
    /// </summary>
    public class BrandModifyStrategy : IExamineStatusStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using (var tran = db.UseTran())
            {
                var brand_query = await db.Queryable<Sys_Brand>().GetFirstAsync(it => it.ID == examine.BusinessID);
                brand_query.UpdateTime = DateTime.Now;

                mapper.Map(examine, brand_query);

                await db.Updateable(brand_query).ExecuteCommandAsync();
                await db.Updateable(examine).ExecuteCommandAsync();

                await db.CommitTranAsync();
            }
        }
    }

    /// <summary>
    /// 品牌删除策略
    /// </summary>
    public class BrandDeleteStrategy : IExamineStatusStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using (var tran = db.UseTran())
            {
                var brand_query = await db.Queryable<Sys_Brand>().GetFirstAsync(it => it.ID == examine.BusinessID);
                await db.Updateable<Sys_Brand>().LogicalDeleteAsync(brand_query.ID);
                await db.Updateable(examine).ExecuteCommandAsync();

                await db.CommitTranAsync();
            }
        }
    }

    /// <summary>
    /// 品牌恢复策略
    /// </summary>
    public class BrandRecoverStrategy : IExamineStatusStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using (var tran = db.UseTran())
            {
                var brand_query = await db.Queryable<Sys_Brand>().GetFirstAsync(it => it.ID == examine.BusinessID);
                await db.Updateable<Sys_Brand>().LogicalRecoverAsync(brand_query.ID);
                await db.Updateable(examine).ExecuteCommandAsync();

                await db.CommitTranAsync();
            }
        }
    }
}
